// 深拷贝和浅拷贝主要是针对对象的属性是对象（引用类型）
var obj = {}
var person = {
  name: '小明',
  hobby: ['学习', ['打球', '购物'], '跑步']
}

function deepClone (obj) {
  if (obj === null) return obj
  if (obj instanceof Date) return new Date(obj)
  if (obj instanceof RegExp) return new RegExp(obj)
  if (typeof obj != 'object') return obj
  var cloneObj = new obj.constructor()
  for (var i in obj) {
    // console.log('i', i)
    if (obj.hasOwnProperty(i)) {
      cloneObj[i] = deepClone(obj[i])
    }
  }
  return cloneObj
}

var person1 = deepClone(person)

person1.name = '小红'
person1.hobby[0] = '跳舞'

console.log(person)
console.log(person1)

var a = []
for (var i = 0; i < 5; i++) {
  // console.log(a)
  (function (i) {
    a[i] = function () {
      console.log(i)
    }
  })(i)
}
a[0]()
a[1]()
a[2]()